var sid = "";
var userName = "";
var role_Id = "";
var userId = "";
var userAccount = "";

String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
    } else {
        return this.replace(reallyDo, replaceWith);
    }
}

function encodeContent(data){
	return encodeURI(data).replace(/&/g,'%26').replace(/\+/g,'%2B').replace(/\s/g,'%20').replace(/#/g,'%23');
}

function myAjax(urls,par,func){	
	// alert(urls+" "+par+" "+func)
	$.ajax({
		url:urls,
		type:"POST",
		data:par,
		async:false,
		dataType:"xml",
		timeout:60000,
		success:func,
		error:function (XMLHttpRequest, textStatus, errorThrown){
			if(XMLHttpRequest.readyState==4){
				alert(urls+":"+XMLHttpRequest.status);
				alert(textStatus+"  " + errorThrown);
			}
		}
	});
}

function arraycontains(obj,p) {
	if (typeof(obj) == 'number'||typeof(obj) == 'string'){
		for(var i in p) {
			if(p[i].indexOf(obj)==0) {
				return i;
			}
		}
		return -1;
	}else{
		return -1;
	}
}

function check_validate1(value){
    // 定义正则表达式部分
    var reg = /^\d+$/;
    if( value.constructor === String ){
        var re = value.match( reg );
        return true;
    }
    return false;
}

function isNum(obj_id,value)
{
	if(check_validate1(value)){
		
	}else{
		alert("请输入数字。");
		$("#obj_id").attr("value","");
	}
}

function checkSessionInside() {
	sid = GetQueryString("sid");
	sid = (sid==null||typeof(sid)=="undefined")?"":sid.replace("#","");
	var par = "sid=" + sid;
	myAjax("../actions/checkSession.jsp?rmd="+new Date().getTime(), par, checkSessionInFolder);
}

function GetQueryString(name)     
{     
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");     
	var r = window.location.search.substr(1).match(reg);     
     if (r!=null){
     	return unescape(r[2]); 
     }else{
     	return null;
     }     
}

function checkSessionInFolder(xml) {
	var status = $(xml).find("CheckSession").attr("Status");
	if (status == "Success") {
		$(xml).find('news').each(function(i) {
			userName = $(this).children('F_NAME').text();
			role_Id = $(this).children('F_ROLE_ID').text();
			userId = $(this).children('F_ID').text();
			userAccount = $(this).children('F_USER_ACCOUNT').text();
		});
	} else {
		var op_info = $(xml).find("CheckSession").attr("op_info");
		alert(op_info);
		window.location.href="../login.html?rmd="+new Date().getTime();
		return false;
	}
}

// 分页代码
var TotalCount=0,TotalPages=0, PageSize = 20, PageNo = 1;

function btnFirst_Click() {
	PageNo = 1;	
	loadList();	
}

function btnPrev_Click() {	
	if(PageNo>1){
		PageNo--;
		loadList();
	}else{
		alert("当前第一页");
	}
}

function btnNext_Click() {	
	if(PageNo<TotalPages){
		PageNo++;
		loadList();
	}else{
		alert("当前最后一页");
	}
}

function btnLast_Click() {	
	PageNo = TotalPages;	
	loadList();	
}

function btnGo_Click() {	
	if ($("#txtPageIndex").val() == "") {	
		$("#txtPageIndex").attr("value", PageNo);	
	} else {	
		if ($("#txtPageIndex").val() <= 0) {	
			$("#txtPageIndex").attr("value", PageNo);	
		} else {	
			if($("#txtPageIndex").val()<=TotalPages){
				PageNo = $("#txtPageIndex").val();
				loadList();	
			}else{
				alert("请输入0-"+TotalPages+"的数字");
				$("#txtPageIndex").attr("value",PageNo);
			}
		}
	}
}

function btnSet_Click() {	
	if ($("#txtPageSize").val() == "") {	
		$("#txtPageSize").attr("value", PageSize);	
	} else {	
		if ($("#txtPageSize").val() <= 0) {	
			$("#txtPageSize").attr("value", PageSize); 	
		}else{	
			PageSize = $("#txtPageSize").val();
			loadList();	
		}	
	}

}

function setPageControl(totalCount) {		
	TotalPages=totalCount/PageSize;
	
	TotalPages=parseInt(TotalPages);
	if(totalCount%PageSize!=0){		
		TotalPages=TotalPages+1;
	}
	
	if(PageNo>TotalPages){
		PageNo = TotalPages;
	}
	
	$("#lblCurrentPage").text(PageNo);	
	$("#lblCountPage").text(TotalPages);	
	$("#lblCount").text(totalCount);	
	$("#txtPageIndex").attr("value", PageNo);	
	$("#txtPageSize").attr("value", PageSize);
	
	if (PageNo == 1) {	
		$("#page_prev").attr("src", "../image/page-prev-disabled.gif");	
		$("#page_prev").attr("disabled", "disabled");	
		$("#page_first").attr("src", "../image/page-first-disabled.gif");	
		$("#page_first").attr("disabled", "disabled");										
	}else {	
		$("#page_prev").attr("src", "../image/page-prev.gif");
		$("#page_prev").attr("disabled", "");	
		$("#page_first").attr("src", "../image/page-first.gif");
		$("#page_first").attr("disabled", "");   			
	}  


	if (PageNo == TotalPages) {		
		$("#page_next").attr("src", "../image/page-next-disabled.gif");		
		$("#page_next").attr("disabled", "disabled");						
		$("#page_last").attr("src", "../image/page-last-disabled.gif");		
		$("#page_last").attr("disabled", "disabled");
	
	}else {		
		$("#page_next").attr("src", "../image/page-next.gif");		
		$("#page_next").attr("disabled", "");				
		$("#page_last").attr("src", "../image/page-last.gif");		
		$("#page_last").attr("disabled", "");		
	}
	if((TotalPages==0)&&(PageNo == 1)){
		$("#page_next").attr("src", "../image/page-next-disabled.gif");	
		$("#page_next").attr("disabled", "disabled");					
		$("#page_last").attr("src", "../image/page-last-disabled.gif");
		$("#page_last").attr("disabled", "disabled");
	}					
}

function setGoToPageNumber(obj){
	var v=$(obj).val();
	if(isNaN(v)){
		$(obj).attr("value",PageNo);
	}
}

function setPageShowNumber(obj){
	var v=$(obj).val();
	if(isNaN(v)){
		$(obj).attr("value",PageSize);
	}
}
//分页代码结束